﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using Model;
using System.Configuration;
using System.Data;

namespace TCDC_System.Attribute.xml
{
    /// <summary>
    /// Summary description for MemberType
    /// </summary>
    public class MemberType : IHttpHandler
    {
        private DBUtil DB;
        XmlTextWriter objX;

        public void ProcessRequest(HttpContext context)
        {

            this.DB = new DBUtil();
            DB.strConn = ConfigurationManager.AppSettings["ConnString"];

            string sql = "SELECT * FROM tcdc_mst_member_type " +
                         "ORDER BY member_type_id ";
            DataTable dt = DB.ExecuteReader(sql);

            context.Response.Clear();
            context.Response.ContentType = "text/xml";

            objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("types");

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {

                    objX.WriteStartElement("type");
                    objX.WriteElementString("type_id", row["member_type_id"].ToString());
                    objX.WriteElementString("type_name_th", row["member_type_name_th"].ToString());
                    objX.WriteElementString("type_name_en", row["member_type_name_en"].ToString());
                    objX.WriteEndElement();
                }
            }
            else 
            {
                objX.WriteStartElement("type");
                objX.WriteElementString("type_id", "");
                objX.WriteElementString("type_name_th", "");
                objX.WriteElementString("type_name_en", "");
                objX.WriteEndElement();
            }


            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}